﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Collections;
using Telerik.Web.UI;
using System.Globalization;

namespace BVVD_Website.userRight
{
    public partial class frmUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvUser.Culture = new CultureInfo("vi-VN");
                grvUser.Rebind();

                grvUser.DataSource = getData();
                grvUser.DataBind();

            }
        }
    
        protected void grvUser_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            grvUser.DataSource = getData();
        }

        private List<LST_NSD> getData()
        {
            BVVDDataStore<LST_NSD> db = new BVVDDataStore<LST_NSD>();
            return db.getFromDB(new Hashtable());
        }

        protected void grvUser_ItemDataBound(object sender, GridItemEventArgs e)
        {
            BVVDDataStore<LST_DANHMUC> type = new BVVDDataStore<LST_DANHMUC>();

            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                RadDropDownList ddlCHUCVU = editFormItem.FindControl("ddlCHUCVU") as RadDropDownList;
                RadTextBox txtMANSD = editFormItem.FindControl("txtMANSD") as RadTextBox;
                RadTextBox txtMATKHAU = editFormItem.FindControl("txtMATKHAU") as RadTextBox;
                

                Hashtable chucvu = new Hashtable();
                chucvu.Add("MALOAIDM", "CHUCVU");
                chucvu.Add("TRANGTHAI", "1");
                ddlCHUCVU.DataSource = type.getFromDB(chucvu);
                ddlCHUCVU.DataTextField = "TENDM";
                ddlCHUCVU.DataValueField = "MADM";


                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    txtMANSD.ReadOnly = false;
                }
                else
                {
                    ddlCHUCVU.SelectedValue = editFormItem["CHUCVU"].Text;                    
                    txtMANSD.ReadOnly = true;
                    txtMATKHAU.TextMode = InputMode.SingleLine;
                    txtMATKHAU.Text = "**********";
                    
                }
                ddlCHUCVU.DataBind();
            }

        }

        protected void grvUser_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            string mansd = item.OwnerTableView.DataKeyValues[item.ItemIndex]["MANSD"].ToString();

            if (mansd.ToUpper() == "Administrator".ToUpper())
            {
                rnAlert.Text = "Administrator không thể xóa!";
            }
            else
            {
                BVVDDataContext db = new BVVDDataContext();
                LST_NSD user = (from dm in db.LST_NSDs
                                where dm.RECID.ToString().Equals(recid)
                                select dm).SingleOrDefault();
                db.LST_NSDs.DeleteOnSubmit(user);
                try
                {
                    db.SubmitChanges();
                    rnAlert.Text = "Xóa thành công người sử dụng có mã <font color='red'>  " + user.MANSD + "</font>";
                }
                catch (Exception ex)
                {
                    rnAlert.Text = "Có lỗi xảy ra. \n <br /> Nguyên nhân : " + ex.Message;
                }
            }
            rnAlert.Show();
        }

        protected void grvUser_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            BVVDLibrary bl = new BVVDLibrary();
            
            LST_NSD user = new LST_NSD
            {
                MANSD = (insertedItem.FindControl("txtMANSD") as RadTextBox).Text,
                TENNSD = (insertedItem.FindControl("txtTENNSD") as RadTextBox).Text,
                MATKHAU = bl.encryptPassword((insertedItem.FindControl("txtMATKHAU") as RadTextBox).Text),
                CHUCVU = (insertedItem.FindControl("ddlCHUCVU") as RadDropDownList).SelectedValue,
                EMAIL = (insertedItem.FindControl("txtEMAIL") as RadTextBox).Text,
                TEL = (insertedItem.FindControl("txtTEL") as RadTextBox).Text,
                MOBILE = (insertedItem.FindControl("txtMOBILE") as RadTextBox).Text,
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();
            db.LST_NSDs.InsertOnSubmit(user);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới người sử dụng có mã <font color='red'> " + user.MANSD + "</font> thành công  ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n <br /> Nguyên nhân : " + ex.Message;

            }
            rnAlert.Show();
        }

        protected void grvUser_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {

            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            BVVDLibrary bl = new BVVDLibrary();
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            LST_NSD user = (from item in db.LST_NSDs
                                 where item.RECID.ToString().Equals(recid)
                                 select item).SingleOrDefault();

            user.TENNSD = (editedItem.FindControl("txtTENNSD") as RadTextBox).Text;
            //user.MATKHAU = bl.encryptPassword((editedItem.FindControl("txtMATKHAU") as RadTextBox).Text);
            user.CHUCVU = (editedItem.FindControl("ddlCHUCVU") as RadDropDownList).SelectedValue;
            user.EMAIL = (editedItem.FindControl("txtEMAIL") as RadTextBox).Text;
            user.TEL = (editedItem.FindControl("txtTEL") as RadTextBox).Text;
            user.MOBILE = (editedItem.FindControl("txtMOBILE") as RadTextBox).Text;            
            user.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            user.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công người sử dụng có mã <font color='red'> " + user.MANSD + "</font>";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br /> Nguyên nhân : " + ex.Message;
            }
            rnAlert.Show();
        }
    }
}